package com.lwl.handler;

import com.lwl.zk.ZookeeperUtils;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

import static com.lwl.boot.ConsumerBoot.connectedServer;

public class ZkNodeWatcher {
    public static void setZkNodeWatcher() {

        ZkClient session = ZookeeperUtils.getSession();
        //设置子节点的变化监听
        session.subscribeChildChanges("rpc-server/adds", new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
                if (list != null && list.size() > 0) {
                    connectedServer(list);

                    System.out.println(s + " 节点的子节点列表变化为：" + list);
                }
            }
        });
    }
}
